package com.yanqu.road.server.cmd.orewar;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.crossorewar.CrossOreStationInfo;
import com.yanqu.road.entity.enums.activity.eActivityOreWarType;
import com.yanqu.road.entity.orewar.OreStationInfo;
import com.yanqu.road.pb.orewar.OreWarProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.orewar.OreWarActivityMgr;
import com.yanqu.road.server.manger.activity.orewar.OreWarMgr;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.server.pb.OreWarPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Cmd(code = Protocol.S_ORE_WAR_STATION_SIMPLE, desc = "矿站简略信息")
public class OreStationSimpleCmd extends AbstractCommand {

	private int activityId;

	private List<OreStationInfo> stationInfoList = new ArrayList<>();
	//跨服简略信息
	private List<CrossOreStationInfo> crossStationInfoList = new ArrayList<>();

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		OreWarProto.OreGetStationSimpleReqMsg reqMsg = OreWarProto.OreGetStationSimpleReqMsg.parseFrom(packet.getBytes());
		activityId = reqMsg.getActivityId();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		ActivityInfo activityInfo = OreWarActivityMgr.getOpenActivityInfo(activityId);
		if(activityInfo != null){
			if(activityInfo.getChildType() == eActivityOreWarType.OreWar.getChildType()){
				Map<Integer, OreStationInfo> oreStationInfoMap = OreWarMgr.getOreStationMap(activityId);
				stationInfoList = new ArrayList<>(oreStationInfoMap.values());
			}else if(activityInfo.getChildType() == eActivityOreWarType.CrossOreWar.getChildType()){
				int groupId = GameUnionActivityGroupMgr.getGroupId(activityId, player.getUserId());
				Map<Integer, CrossOreStationInfo> crossOreStationInfoMap = OreWarMgr.getCrossOreStationMap(activityId,groupId);
				crossStationInfoList = new ArrayList<>(crossOreStationInfoMap.values());
			}
		}

	}

	@Override
	public void sendData(GamePlayer player) {
		OreWarProto.OreStationListMsg.Builder respMsg = OreWarPb.parseOreStationListMsg(player.getUserId(),stationInfoList);
		if(crossStationInfoList.size() > 0){
			respMsg = OreWarPb.parseCrossOreStationListMsg(player.getUserId(),crossStationInfoList);
		}
		player.sendPacket(Protocol.U_ORE_WAR_STATION_SIMPLE, respMsg);
	}
}
